package com.daoxiang.controller;

import com.daoxiang.pojo.*;
import com.daoxiang.service.*;
import com.daoxiang.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserCustomService userCustomService;

    /**
     * 前台：根据前端传来的 username,password 查询用户，成功返回首页，否则返回用户登录页
     * @Author wml
     * @param username
     * @param password
     * @return ModelAndView
     */
    @GetMapping("/getUserByAccountAndPassword")
    public ModelAndView getUserByAccountAndPassword(String username, String password, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        List<User> users = this.userService.getUserByAccountAndPassword(username, password);
        if(users != null && users.size() > 0) {
            session.setAttribute("loginUser",users.get(0));
            modelAndView.setViewName("reception/index");
        } else {
            modelAndView.addObject("error", "用户名或密码错误！");
            modelAndView.setViewName("reception/login");
        }
        return modelAndView;
    }

    /**
     * 前台：根据前端传来的 username,password 新增用户，成功返回用户登录也，否则返回用户注册页
     * @Author wml
     * @param username
     * @param password
     * @return ModelAndView
     * @throws InterruptedException
     */
    @PostMapping("/register")
    public ModelAndView register(String username, String password) throws InterruptedException {
        ModelAndView modelAndView = new ModelAndView();
        List<User> users = this.userService.getUserByAccount(username);
        if(users != null && users.size() > 0) {
            modelAndView.addObject("error", "用户名已存在！");
            modelAndView.setViewName("reception/register");
        } else {
            User user = new User();
            user.setAccount(username);
            user.setPassword(password);
            user.setNickname(username);
            user.setPhoto("userInfoUploadFile\\aaa.jpg");
            int i = this.userService.saveUser(user);
            modelAndView.setViewName("reception/register_result");
        }
        return modelAndView;
    }

    // 在RESTFul编程中，只要controller中的方法返回的方法结果类型为 ResponseEntity 则，该方法就会进行ajax处理
    // 不会进行默认的视图解析
    /**
     * 后台：根据前端传来的用户条件，查询符合这些信息的所有的用户
     * @Author wml
     * @param userCustom
     * @return 返回一个 JSON 格式的用户集合
     * @throws InterruptedException
     */
    @GetMapping("/queryAllUsers")
    public ResponseEntity<List<UserCustom>> queryAllUsers(UserCustom userCustom) throws InterruptedException {
        // 调用service层进行查询操作
        List<UserCustom> userCustomList = this.userCustomService.queryUserParams(userCustom);
        List<UserCustom> userCustomList2 = this.userCustomService.queryUserParams2();
        for (UserCustom uc: userCustomList2) {
            userCustomList.add(uc);
        }
        System.out.println(userCustomList.size());
        TimeUnit.MILLISECONDS.sleep(100);
        return new ResponseEntity<List<UserCustom>>(userCustomList, HttpStatus.OK);
    }

    @GetMapping("/queryUserByUid")
    public ResponseEntity<UserCustom> queryUserByUid(UserCustom userCustom) {
        return new ResponseEntity<UserCustom>(this.userCustomService.queryUserByUid(userCustom), HttpStatus.OK);
    }

    /**
     * 后台：根据前端传来的信息新增一个用户
     * @Author wml
     * @param user
     * @param multipartFile
     * @return 返回一个提示信息
     */
    @PostMapping("/ajaxAddUser")
    public ResponseEntity<String> ajaxAddUser(User user, @RequestParam("file_photo") MultipartFile multipartFile) {
        List<User> users = this.userService.getUserByAccount(user.getAccount());
        if(users != null && users.size() > 0) {
            return new ResponseEntity<String>("AlreadyExists", HttpStatus.OK);
        }
        // 调用utils层的方法进行文件上传处理进行查询操作
        MyUtils.doSingleFileUpload(multipartFile, "userInfoUploadFile", "userinfo-", user, "photo");
        // 调用service层进行查询操作
        int i = this.userService.saveUser(user);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

    /**
     * 后台：根据前端传来的信息删除一个用户
     * @Author wml
     * @param deleteUid
     * @return 返回一个提示信息
     */
    @DeleteMapping("/ajaxDeleteUser")
    public ResponseEntity<String> ajaxDeleteUser(int deleteUid) {
        int i = this.userService.deleteUser(deleteUid);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

    /**
     * 后台：根据前端传来的信息修改一个用户
     * @Author wml
     * @param user
     * @param multipartFile
     * @return 返回一个提示信息
     */
    @PutMapping("/ajaxEditUser")
    public ResponseEntity<String> ajaxEditUser(User user, @RequestParam("file_photo") MultipartFile multipartFile, String beforeEditAccount) {
        if (!user.getAccount().equals(beforeEditAccount)) {
            List<User> users = this.userService.getUserByAccount(user.getAccount());
            if(users != null && users.size() > 0) {
                return new ResponseEntity<String>("AlreadyExists", HttpStatus.OK);
            }
        }
        if (multipartFile.getSize() != 0) {
            MyUtils.doSingleFileUpload(multipartFile, "userInfoUploadFile", "userinfo-", user, "photo");
        }
        int i = this.userService.updateUser(user);
        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }
}
